package com.homeassistant.application;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.LocalActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.IntentSender.SendIntentException;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.homeassistant.application.R;

public class HomeActivity extends Activity{

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
//			isFinish = true;
//			Toast.makeText(HomeActivity.this,"再按一次返回键退出程序!",Toast.LENGTH_LONG).show();
//			return false;
			AlertDialog.Builder dialog = new Builder(HomeActivity.this);
			dialog.setMessage("确定要退出吗?");
			dialog.setTitle("提示");
			dialog.setPositiveButton("确定", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
					HomeActivity.this.finish();
				}
			});
			dialog.setNegativeButton("取消", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			dialog.create().show();
		}
//		if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0 && isFinish){
//			HomeActivity.this.finish();
//		}
		return super.onKeyDown(keyCode, event);
	}

	//页面数组
	private ArrayList<View> mPagerList;
	
	private ViewPager mPager;
	
	private boolean isFinish = false;
	
	private LocalActivityManager manager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		manager = new LocalActivityManager(this, true);
		manager.dispatchCreate(savedInstanceState);
		InitViewPager();
	}

	private View getView(String id, Intent intent) {
		return manager.startActivity(id, intent).getDecorView();
	}
	
	private void InitViewPager(){
		mPagerList = new ArrayList<View>();
		mPagerList.add(getView(Integer.toString(R.layout.index),new Intent(HomeActivity.this,IndexActivity.class)));
		mPagerList.add(getView(Integer.toString(R.layout.accounting),new Intent(HomeActivity.this,AccountingActivity.class)));
		mPagerList.add(getView(Integer.toString(R.layout.fashion),new Intent(HomeActivity.this,FashionActivity.class)));
		mPagerList.add(getView(Integer.toString(R.layout.bankcard),new Intent(HomeActivity.this,BankcardActivity.class)));
		mPagerList.add(getView(Integer.toString(R.layout.more),new Intent(HomeActivity.this,MoreActivity.class)));
		
		RadioGroup rg = (RadioGroup)findViewById(R.id.rgbutton);
		rg.setOnCheckedChangeListener(new RadioOnClickedChangeListener());
		mPager = (ViewPager) findViewById(R.id.vPager);
		mPager.setAdapter(new MPagerAdapter());
		mPager.setOnPageChangeListener(new GuidePageChangeListener());
	}
	
	public class RadioOnClickedChangeListener implements OnCheckedChangeListener{

		LayoutInflater inflater = getLayoutInflater();
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch(checkedId){
			case R.id.indexRadio:
				mPagerList.set(0, getView(Integer.toString(R.layout.index),new Intent(HomeActivity.this,IndexActivity.class)));
				mPager.setCurrentItem(0);
				break;
			case R.id.accoutingRadio:
				mPagerList.set(1, getView(Integer.toString(R.layout.accounting),new Intent(HomeActivity.this,AccountingActivity.class)));
				mPager.setCurrentItem(1);
				break;
			case R.id.fashionRadio:
				mPagerList.set(2, getView(Integer.toString(R.layout.fashion),new Intent(HomeActivity.this,FashionActivity.class)));
				mPager.setCurrentItem(2);
				break;
			case R.id.bankcardRadio:
				mPagerList.set(3, getView(Integer.toString(R.layout.bankcard),new Intent(HomeActivity.this,BankcardActivity.class)));
				mPager.setCurrentItem(3);
				break;
			case R.id.moreRadio:
				mPagerList.set(4, getView(Integer.toString(R.layout.more),new Intent(HomeActivity.this,MoreActivity.class)));
				mPager.setCurrentItem(4);
				break;
			default:
				break;
			}
		}
		
	}
	
	public class MPagerAdapter extends PagerAdapter{
		
		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager)container).removeView(mPagerList.get(position));
		}
		
		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager)container).addView(mPagerList.get(position));
			return mPagerList.get(position);
		}
		
		@Override
		public int getCount() {
			return mPagerList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (View)arg1;
		}
		
	}
	
	public class GuidePageChangeListener implements OnPageChangeListener{
        @Override
        public void onPageSelected(int position) {
        	RadioGroup rg = (RadioGroup)findViewById(R.id.rgbutton);
        	switch (position){
        	case 0:
        		rg.check(R.id.indexRadio);
        		break;
        	case 1:
        		rg.check(R.id.accoutingRadio);
        		break;
        	case 2:
        		rg.check(R.id.fashionRadio);
        		break;
        	case 3:
        		rg.check(R.id.bankcardRadio);
        		break;
        	case 4:
        		rg.check(R.id.moreRadio);
        		break;
        	default:
				break;
        	}
        }

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}
    }   
}